<template>
  <div class="login-container">
    <!-- 外层大盒子 -->
    <div class="login-box">
      <!-- 图片盒子 -->
      <div class="pic-box">
        <img src="@/assets/login.jpg" alt="">
      </div>
      <!-- 图片盒子 -->
      <!-- 表单区域 -->
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入用户昵称"
            name="username"
            type="text"
          />
        </el-form-item>
        <!-- 用户名 -->

        <!-- 密码 -->
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            @keyup.enter.native="handleLogin"
          />
          <!-- 密码 -->

          <span class="show-pwd">
            <svg-icon
              :icon-class="
                passwordType === 'password' ? 'eye' : 'eye-open'
              "
            />
          </span>

          <!-- 登录 重置按钮 -->
        </el-form-item>
        <div class="el-from-item-content">
          <el-button
            :loading="loading"
            type="primary"
            label="right"
            @click.native.prevent="handleLogin"
          >登录</el-button>
          <el-button
            :loading="loading"
            label="right"
            @click.native.prevent="loginForm = {}"
          >重置</el-button>
        </div>
      </el-form>
      <!-- 表单区域 -->
    </div>
    <!-- 外层大盒子 -->
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data() {
    return {
      labelPosition: 'right',
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在2-6个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3-10个字符', trigger: 'blur' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    ...mapActions(['user/login']),
    handleLogin() {
      this.$refs.loginForm.validate(async isOk => {
        if (isOk) {
          try {
            this.loading = true
            // 校验通过 调用actions
            await this['user/login'](this.loginForm)
            // 跳转到首页
            this.$router.push('/')
          } catch (error) {
            console.log(error)
          } finally {
            // 不论 try catch 都会去关闭圈圈
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  position: relative;
  .login-box {
    width: 450px;
    height: 310px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .pic-box {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 152px;
    height: 152px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
  .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;

    input {
      background: #fff;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      padding: 0 20px;
      color: #606266;
      height: 40px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #dcdfe6;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    // height: 40px;
  }
  .el-from-item-content {
    padding-left: 260px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2b4b6b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: absolute;
    width: 450px;
    height: 200px;
    max-width: 100%;
    padding: 0 20px;
    bottom: 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    // cursor: pointer;
    user-select: none;
  }
}
</style>
